package com.capinfo.accounting.web;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.NativeWebRequest;

import com.capinfo.accumulation.model.accounting.Fzlxsz;
import com.capinfo.accumulation.model.accounting.mybist.GB_JZPZXX;
import com.capinfo.accumulation.model.accounting.mybist.KEMU_LEIBIE;
import com.capinfo.accumulation.model.accounting.mybist.KMSZJBSJ;
import com.capinfo.accumulation.model.accounting.mybist.KMSZSPLSSJ;
import com.capinfo.accumulation.model.accounting.mybist.KMSZXGLSSJ;
import com.capinfo.accumulation.model.enums.SjztEnums;
import com.capinfo.accumulation.model.enums.SpztEnums;
import com.capinfo.accumulation.model.pagemodel.Json;
import com.capinfo.accumulation.parameter.accounting.DataGrid;
import com.capinfo.accumulation.service.accounting.FzlxszService;
import com.capinfo.accumulation.service.accounting.ZtszService;
import com.capinfo.accumulation.service.accounting.mybits.GB_JZPZXXServiceI;
import com.capinfo.accumulation.service.accounting.mybits.KEMU_LEIBIEServiceI;
import com.capinfo.accumulation.service.accounting.mybits.KMSZJBSJServiceI;
import com.capinfo.accumulation.service.accounting.mybits.KMSZSPLSSJServiceI;
import com.capinfo.accumulation.service.accounting.mybits.KMSZXGLSSJServiceI;
import com.capinfo.accumulation.service.accounting.mybits.ZTJBSJServiceI;
import com.capinfo.commons.project.model.security.SystemUser;
import com.capinfo.components.security.web.utils.WebRequestUtils;
import com.capinfo.framework.model.dictionary.Dictionary;

/**
 *  @author zxl
 */
@Controller
@RequestMapping("/business/accounting/kMSZJBSJ")
public class KMSZJBSJController extends BaseController {
	@Autowired
	public KMSZJBSJServiceI kMSZJBSJService;
	@Autowired
	public GB_JZPZXXServiceI gb_jzpzxxservicei;
	
	@Autowired
	public KMSZXGLSSJServiceI kMSZXGLSSJService;
	@Autowired
	public KMSZSPLSSJServiceI kmszsplssjService;
	@Autowired
	public ZTJBSJServiceI zTJBSJService;
	@Autowired
	private ZtszService ztszService; 
	@Autowired
    private FzlxszService fzlxszService;
	@Autowired
	private KEMU_LEIBIEServiceI  kEMU_LEIBIEService;
	@RequestMapping("/dataGrid.shtml")
	@ResponseBody
	public DataGrid dataGrid(@RequestParam  Map param) {
		return kMSZXGLSSJService.findbyByGrid(param);
	}
	
	@RequestMapping("/add.shtml")
	@ResponseBody
	public Json add(KMSZJBSJ t, String[] fzhs,  HttpSession session,NativeWebRequest request) {
		
		Json j = new Json();
		try {
		
			Map  pkmMap = new HashMap();
			Date time = new Date();
			pkmMap.put("KMBH_EQ_STR", t.getPKMBH());
			DataGrid gridPkm = kMSZJBSJService.findbyByGrid(pkmMap);
			KMSZJBSJ pkm = null;
			if(gridPkm.getRows()!=null && gridPkm.getRows().size()>0){
				 pkm = (KMSZJBSJ)gridPkm.getRows().get(0);
			 }
			if(pkm!= null){
				t.setKMBH(pkm.getKMBH()+t.getKMBH());
			}
			t.setZT((long)SjztEnums.新增审批中.getToken());
			t.setSPZT((long)SpztEnums.待审批.getToken());
			t.setJBJG("上海市住房公积金");
			t.setCRSJ(time);
			
			SystemUser user = (SystemUser)WebRequestUtils.retrieveSecureUser(request);
			kMSZJBSJService.insert(t);
			KMSZXGLSSJ kmszxglssj = new KMSZXGLSSJ();
			BeanUtils.copyProperties(t, kmszxglssj);
			kmszxglssj.setKMSZID(t.getID());
			kmszxglssj.setCZY(user.getName().toString());
			kmszxglssj.setCRSJ(time);
			kMSZXGLSSJService.insert(kmszxglssj);
//			kmszxglssj.setZT((long)SjztEnums.新增审批中.getToken());
//			kmszxglssj.setSPZT((long)SpztEnums.待审批.getToken());
			if(fzhs.length>0){
				for(int i =0 ; i< fzhs .length ; i++){
					KEMU_LEIBIE kemu_leibie = new KEMU_LEIBIE();
					kemu_leibie.setKEMUID(t.getID()+"");
					kemu_leibie.setLEIXINGID(fzhs[i]);
					this.kEMU_LEIBIEService.insert(kemu_leibie);
				}
			}
			j.setSuccess(true);
			j.setMsg("新增成功");
			j.setObj(t);
		} catch (Exception e) {
			 e.printStackTrace();
			j.setMsg(e.getMessage());
		}
		return j;
	}

	
	@RequestMapping("/edit.shtml")
	@ResponseBody
	public Json edit(KMSZXGLSSJ t , HttpSession session,NativeWebRequest request) {
		SystemUser user = (SystemUser)WebRequestUtils.retrieveSecureUser(request);
		Date time = new Date();
		t.setCZY(user.getName().toString());
		t.setCRSJ(time);
		t.setKMBH(t.getPKMBH()+t.getKMBH());
		Json j = new Json();
		if(t.getZT()==2){
			j.setMsg("只允许修改科目名称！");
			j.setObj(t);
		}else{
		try {
			kMSZXGLSSJService.updateByPrimaryKeySelective(t);
			j.setSuccess(true);
			j.setMsg("修改成功");
			j.setObj(t);
		} catch (Exception e) {
			 e.printStackTrace();
			j.setMsg(e.getMessage());
		}
	}
		return j;
	}
	@RequestMapping("/delete.shtml")
	@ResponseBody
	public Json delete(String ID,String KMSZID) {
		Json j = new Json();
		try {
			kMSZXGLSSJService.deleteByPrimaryKey(ID);
			kMSZJBSJService.deleteByPrimaryKey(KMSZID);
			j.setSuccess(true);
			j.setMsg("删除成功");
		} catch (Exception e) {
			 e.printStackTrace();
			j.setMsg(e.getMessage());
		}
		return j;
	}

	@RequestMapping("/selectByid.shtml")
	@ResponseBody
	public KMSZJBSJ selectByid(String id) {
		return kMSZJBSJService.selectByPrimaryKey(id);
	}
	
	
	/**
	 * 
	 * @return
	 */
	@RequestMapping("/manager.shtml")
	public String manager() {
		return "/business/accounting/kMSZJBSJ/kMSZJBSJ";
	}
	
	
	/**
	 * 
	 * @return
	 */
	@RequestMapping("/addPage.shtml")
	public String addPage(String kmid , HttpServletRequest request) {
		KMSZXGLSSJ pkmszxglssj = new KMSZXGLSSJ();
		if(kmid!=null) {
			pkmszxglssj = this.kMSZXGLSSJService.selectByPrimaryKey(kmid);
		}
		request.setAttribute("pkm", pkmszxglssj);
		KMSZXGLSSJ KMSZXGLSSJ  = new KMSZXGLSSJ();
		if(pkmszxglssj!=null && pkmszxglssj.getKMJB() != null){
			KMSZXGLSSJ.setKMJB(pkmszxglssj.getKMJB()+1);
		}else{
			KMSZXGLSSJ.setKMJB(1L);
		}
		request.setAttribute("KMSZXGLSSJ", KMSZXGLSSJ);
		Map map = new HashMap();
		DataGrid ztgrid = zTJBSJService.findbyByGrid(map);
		request.setAttribute("ztlist", ztgrid.getRows());
		Set<Dictionary> paymentSiteList   = ztszService.getDictionariesBySortName("余额方向");
		request.setAttribute("yefxlist", paymentSiteList);
	    Set<Dictionary> paymentSiteList1 = ztszService.getDictionariesBySortName("科目属性");
        request.setAttribute("kmsxlist", paymentSiteList1);
        Set<Dictionary> paymentSiteList2 = ztszService.getDictionariesBySortName("台账标记");
        request.setAttribute("tzbjlist", paymentSiteList2);
	    Set<Dictionary> paymentSiteList3 = ztszService.getDictionariesBySortName("日记账类型");
        request.setAttribute("rjzlxlist", paymentSiteList3);
	    //List<Ztsz> paymentSiteList4 = ztszService.getZtsz();
	    Set<Dictionary> paymentSiteList5 = ztszService.getDictionariesBySortName("末级标记");
	    request.setAttribute("mjbjlist", paymentSiteList5);
	    Set<Dictionary> paymentSiteList6 = ztszService.getDictionariesBySortName("会计年度");
	    request.setAttribute("kjndlist", paymentSiteList6);   
	    List<Fzlxsz> fzhslxList = fzlxszService.getFzlxsz();
	    request.setAttribute("fzlxszlist", fzhslxList);   
		return "/business/accounting/kMSZJBSJ/kMSZJBSJAdd";
	}
	
	/**
	 * 
	 * @return
	 */
	@RequestMapping("/editPage.shtml")
	public String editPage(String id ,HttpServletRequest request) { 
		KMSZXGLSSJ kmszxglssj = this.kMSZXGLSSJService.selectByPrimaryKey(id);
		if(kmszxglssj.getPKMBH()!= null){
			kmszxglssj.setKMBH(kmszxglssj.getKMBH().replace(kmszxglssj.getPKMBH(), ""));
			
		}
		request.setAttribute("KMSZXGLSSJ", kmszxglssj);
		KMSZXGLSSJ pkm =  new KMSZXGLSSJ();
		Map  pkmMap = new HashMap();
		if(kmszxglssj.getPKMBH() != null){
			pkmMap.put("KMBH_EQ_STR", kmszxglssj.getPKMBH());
			DataGrid gridPkm = kMSZXGLSSJService.findbyByGrid(pkmMap);
			if(gridPkm.getRows()!=null && gridPkm.getRows().size()>0){
				 pkm = (KMSZXGLSSJ)gridPkm.getRows().get(0);
			 }
		}
		request.setAttribute("pkm", pkm);
		Map map = new HashMap();
		DataGrid ztgrid = zTJBSJService.findbyByGrid(map);
		request.setAttribute("ztlist", ztgrid.getRows());
		Set<Dictionary> paymentSiteList   = ztszService.getDictionariesBySortName("余额方向");
		request.setAttribute("yefxlist", paymentSiteList);
		Set<Dictionary> paymentSiteList1 = ztszService.getDictionariesBySortName("科目属性");
		request.setAttribute("kmsxlist", paymentSiteList1);
		Set<Dictionary> paymentSiteList2 = ztszService.getDictionariesBySortName("台账标记");
		request.setAttribute("tzbjlist", paymentSiteList2);
		Set<Dictionary> paymentSiteList3 = ztszService.getDictionariesBySortName("日记账类型");
		request.setAttribute("rjzlxlist", paymentSiteList3);
		//List<Ztsz> paymentSiteList4 = ztszService.getZtsz();
		Set<Dictionary> paymentSiteList5 = ztszService.getDictionariesBySortName("末级标记");
		request.setAttribute("mjbjlist", paymentSiteList5);
		Set<Dictionary> paymentSiteList6 = ztszService.getDictionariesBySortName("会计年度");
		request.setAttribute("kjndlist", paymentSiteList6);   
		List<Fzlxsz> fzhslxList = fzlxszService.getFzlxsz();
		request.setAttribute("fzlxszlist", fzhslxList);   
		Map  temp = new HashMap();
		KMSZXGLSSJ tkmszxg = this.kMSZXGLSSJService.selectByPrimaryKey(id);
		String ptemp ="";
		temp.put("PKMBH_EQ_STR", tkmszxg.getKMBH());
		DataGrid gridPkm = kMSZXGLSSJService.findbyByGrid(temp);
		Map  tempgb = new HashMap();
		tempgb.put("KMBH_EQ_STR",tkmszxg.getKMBH());
		DataGrid gridPgb = gb_jzpzxxservicei.findbyByGrid(tempgb);
		if(gridPkm.getRows()!=null && gridPkm.getRows().size()>0||gridPgb.getRows()!=null && gridPgb.getRows().size()>0){
		
			ptemp="1";
		}else if(gridPkm.getRows()==null && gridPkm.getRows().size()>0&&gridPgb.getRows()==null && gridPgb.getRows().size()==0){
			ptemp="2";
		}
		request.setAttribute("ptemp", ptemp);
		return "/business/accounting/kMSZJBSJ/kMSZJBSJEdit";
	}
	/**
	 * 
	 * @return
	 */
	@RequestMapping("/viewPage.shtml")
	public String viewPage(String id ,HttpServletRequest request) {
		KMSZJBSJ kMSZJBSJ = this.kMSZJBSJService.selectByPrimaryKey(id);
		request.setAttribute("kMSZJBSJ", kMSZJBSJ);
		return "/business/accounting/kMSZJBSJ/kMSZJBSJView";
	}
	/**
	 * 新增经办页面
	 * @return
	 */
	@RequestMapping("/spPage.shtml")
	public String spPage(String id ,HttpServletRequest request) {
		KMSZJBSJ kMSZJBSJ = this.kMSZJBSJService.selectByPrimaryKey(id);
		request.setAttribute("kMSZJBSJ", kMSZJBSJ);
		return "/business/accounting/kMSZJBSJ/kMSZJBSP";
	}
	/**
	 * 禁用经办页面
	 * @return
	 */
	@RequestMapping("/jyPage.shtml")
	public String jyPage(String id ,HttpServletRequest request) {
		KMSZJBSJ kMSZJBSJ = this.kMSZJBSJService.selectByPrimaryKey(id);
		request.setAttribute("kMSZJBSJ", kMSZJBSJ);
		return "/business/accounting/kMSZJBSJ/kMSZJBJY";
	}
	/**
	 * 禁用申请
	 * @return
	 */
	@RequestMapping("/jySQ.shtml")
	@ResponseBody
	public Json jySQ(String id ,HttpServletRequest request) {
		Json j = new Json();
		KMSZJBSJ kMSZJBSJ = this.kMSZJBSJService.selectByPrimaryKey(id);
		if(kMSZJBSJ.getZT()== SjztEnums.停用审批中.getToken()) {
			System.out.println("请勿重复申请");
		}else {
	    kMSZJBSJ.setZT((long) SjztEnums.停用审批中.getToken());
		
		try {
			 kMSZJBSJService.updateByPrimaryKeySelective(kMSZJBSJ);
			j.setSuccess(true);
		} catch (Exception e) {
			 e.printStackTrace();
			j.setMsg(e.getMessage());
		}
		
		return j;
		}
		j.setSuccess(false);
		return j;
	}
	/**
	 * 禁用取消
	 * @return
	 */
	@RequestMapping("/jinyongQX.shtml")
	@ResponseBody
	public Json jinyongQX(String id ,HttpServletRequest request) {
		KMSZJBSJ kMSZJBSJ = this.kMSZJBSJService.selectByPrimaryKey(id);
	    kMSZJBSJ.setZT((long) SjztEnums.正常使用.getToken());
		Json j = new Json();
		try {
			 kMSZJBSJService.updateByPrimaryKeySelective(kMSZJBSJ);
			j.setSuccess(true);
			j.setMsg("取消成功");
		} catch (Exception e) {
			 e.printStackTrace();
			j.setMsg(e.getMessage());
		}
		return j;
	}
	/**
	 * 新增经办审批
	 * @return
	 */
	@RequestMapping("/xzsp.shtml")
	@ResponseBody
	public Json xzsp(KMSZSPLSSJ t ,String TG , HttpSession session,NativeWebRequest request) {
		SystemUser user = (SystemUser)WebRequestUtils.retrieveSecureUser(request);
		Json j = new Json();
		t.setCZY(user.getLogonName());
		t.setCZSJ(new Date());
		t.setSPR(user.getLogonName());
		t.setSPSJ(new Date());
		Map map = new HashMap();
		map.put("KMSZID_EQ_NUM", t.getKMSZLS());
		map.put("sort", "ID");
		map.put("order", "DESC");
		map.put("currentPieceNum", 1);
		map.put("perPieceSize", 1);
		KMSZXGLSSJ kmszxglssj  = (KMSZXGLSSJ)this.kMSZXGLSSJService.findbyByGrid(map).getRows().get(0);
		kmszxglssj.setID(t.getID());
		KMSZJBSJ kmszjbsj = new KMSZJBSJ();
		kmszjbsj.setID(t.getKMSZLS());
		t.setKMSZLS(kmszxglssj.getID());
		if("通过".equals(TG)){
			t.setSPZT((long)SpztEnums.初审通过.getToken());
			t.setSJZT((long)SjztEnums.正常使用.getToken());
			kmszxglssj.setSPZT((long)SpztEnums.初审通过.getToken());
			kmszxglssj.setZT((long)SjztEnums.正常使用.getToken());
			kmszjbsj.setZT((long)SjztEnums.正常使用.getToken());
		}else{
			t.setSPZT((long)SpztEnums.初审不通过.getToken());
			t.setSJZT((long)SjztEnums.修改审批中.getToken());
			kmszxglssj.setSPZT((long)SpztEnums.初审不通过.getToken());
			kmszxglssj.setZT((long)SjztEnums.修改审批中.getToken());
			kmszjbsj.setZT((long)SjztEnums.修改审批中.getToken());
		}
		this.kMSZXGLSSJService.updateByPrimaryKeySelective(kmszxglssj);
		this.kMSZJBSJService.updateByPrimaryKeySelective(kmszjbsj);
		try {
			kmszsplssjService.insert(t);
			j.setSuccess(true);
			j.setMsg("修改成功");
			j.setObj(t);
		} catch (Exception e) {
			 e.printStackTrace();
			j.setMsg(e.getMessage());
		}
		return j;
	}
	/**
	 * 禁用经办功能
	 * @return
	 */
	@RequestMapping("/jyjb.shtml")
	@ResponseBody
	public Json jyjb(KMSZSPLSSJ t ,String TG , HttpSession session,NativeWebRequest request) {
		SystemUser user = (SystemUser)WebRequestUtils.retrieveSecureUser(request);
		Json j = new Json();
		t.setCZY(user.getLogonName());
		t.setCZSJ(new Date());
		t.setSPR(user.getLogonName());
		t.setSPSJ(new Date());
		Map map = new HashMap();
		map.put("KMSZID_EQ_NUM", t.getKMSZLS());
		map.put("sort", "ID");
		map.put("order", "DESC");
		map.put("currentPieceNum", 1);
		map.put("perPieceSize", 1);
		KMSZXGLSSJ kmszxglssj  = (KMSZXGLSSJ)this.kMSZXGLSSJService.findbyByGrid(map).getRows().get(0);
		kmszxglssj.setID(t.getID());
		KMSZJBSJ kmszjbsj = new KMSZJBSJ();
		kmszjbsj.setID(t.getKMSZLS());
		t.setKMSZLS(kmszxglssj.getID());
		if("通过".equals(TG)){
			t.setSPZT((long)SpztEnums.初审通过.getToken());
			t.setSJZT((long)SjztEnums.禁用中.getToken());
			kmszxglssj.setSPZT((long)SpztEnums.初审通过.getToken());
			kmszxglssj.setZT((long)SjztEnums.禁用中.getToken());
			kmszjbsj.setZT((long)SjztEnums.禁用中.getToken());
		}else{
			t.setSPZT((long)SpztEnums.初审不通过.getToken());
			t.setSJZT((long)SjztEnums.停用审批中.getToken());
			kmszxglssj.setSPZT((long)SpztEnums.初审不通过.getToken());
			kmszxglssj.setZT((long)SjztEnums.停用审批中.getToken());
			kmszjbsj.setZT((long)SjztEnums.停用审批中.getToken());
		}
		this.kMSZXGLSSJService.updateByPrimaryKeySelective(kmszxglssj);
		this.kMSZJBSJService.updateByPrimaryKeySelective(kmszjbsj);
		try {
			kmszsplssjService.insert(t);
			j.setSuccess(true);
			j.setMsg("修改成功");
			j.setObj(t);
		} catch (Exception e) {
			 e.printStackTrace();
			j.setMsg(e.getMessage());
		}
		return j;
	}
	/**
	 * 禁用经办
	 * @return
	 */
	@RequestMapping("/jinyongJB.shtml")
	@ResponseBody
	public Json jinyongJB(KMSZSPLSSJ t ,String id, HttpSession session,NativeWebRequest request) {
		SystemUser user = (SystemUser)WebRequestUtils.retrieveSecureUser(request);
		Json j = new Json();
		t.setCZY(user.getLogonName());
		t.setCZSJ(new Date());
		t.setSPR(user.getLogonName());
		t.setSPSJ(new Date());
		Map map = new HashMap();
		map.put("KMSZID_EQ_NUM", t.getKMSZLS());
		map.put("sort", "ID");
		map.put("order", "DESC");
		map.put("currentPieceNum", 1);
		map.put("perPieceSize", 1);
		KMSZXGLSSJ kmszxglssj  = (KMSZXGLSSJ)this.kMSZXGLSSJService.findbyByGrid(map).getRows().get(0);
		kmszxglssj.setID(t.getID());
		KMSZJBSJ kmszjbsj = new KMSZJBSJ();
		kmszjbsj.setID(t.getKMSZLS());
		t.setKMSZLS(kmszxglssj.getID());
		if(id !=null ){
			t.setSPZT((long)SpztEnums.初审通过.getToken());
			t.setSJZT((long)SjztEnums.禁用中.getToken());
			kmszxglssj.setSPZT((long)SpztEnums.初审通过.getToken());
			kmszxglssj.setZT((long)SjztEnums.禁用中.getToken());
			kmszjbsj.setZT((long)SjztEnums.禁用中.getToken());
		}
		this.kMSZXGLSSJService.updateByPrimaryKeySelective(kmszxglssj);
		this.kMSZJBSJService.updateByPrimaryKeySelective(kmszjbsj);
		try {
			kmszsplssjService.insert(t);
			j.setSuccess(true);
			j.setMsg("修改成功");
			j.setObj(t);
		} catch (Exception e) {
			 e.printStackTrace();
			j.setMsg(e.getMessage());
		}
		return j;
	}
	/**
	 * 历史记录页面
	 * @return
	 */
	@RequestMapping("/kmszlssj.shtml")
	public String lssj(String id,HttpServletRequest request  ) {
		request.setAttribute("id", id);
		return "/business/accounting/kMSZJBSJ/kMSZLSSJ";
	}
	
	
	/**
	 * 审批历史记录页面
	 * @return
	 */
	@RequestMapping("/kMSZLSSJspls.shtml")
	public String kMSZLSSJspls(String id,HttpServletRequest request  ) {
		request.setAttribute("id", id);
		return "/business/accounting/kMSZJBSJ/kMSZLSSJspls";
	}
	/**
	 * 
	 * @return
	 */
	@RequestMapping("/kMSZsplsdatagrid.shtml")
	@ResponseBody
	public DataGrid splsdataGrid(@RequestParam  Map param) {
		return kmszsplssjService.findbyByGrid(param);
	}
	/**
	 * 
	 * @return
	 */
	@RequestMapping("/lssjdataGrid.shtml")
	@ResponseBody
	public DataGrid lssjdataGrid(@RequestParam  Map param) {
		return kMSZXGLSSJService.findbyByGrid(param);
	}
}
